package com.yty.demo._7_thread;

/**
 * @author: yangtaoyao
 * @time: 2020/1/13 15:16
 * @description: yield() join()
 */
public class ThreadTest {
    public static void main(String[] args) {
        Thread t1=new TestThread1();
        System.out.println(t1.getPriority());

        TestThread2 testThread2=new TestThread2();
        Thread t2=new Thread(testThread2);
//        t2.setPriority(10);
        t1.start();
        t2.start();

        System.out.println("----main1");
        try {
            System.out.println("t1.join()");
            t2.join();//等待线程结束，阻塞当前main方法，在这块把t1的run执行完后再继续执行main剩下的代码；
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("----main2");
        System.out.println("----main3");

    }
}

class TestThread1 extends Thread {
    @Override
    public void run() {
        System.out.println("TestThread1运行");
        for (int i = 0; i < 5; i++) {
            if(i%2==0){
//                System.out.println("TestThread1-线程让步");
//                Thread.yield();//线程让步,让出时间片
            }
            System.out.println("TestThread1 "+i);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class TestThread2 implements Runnable{
    @Override
    public void run() {
        System.out.println("TestThread2运行");
        for (int i = 0; i < 5; i++) {
            System.out.println("TestThread2 "+i);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
